let g = {
  ajax (options) {
    return $.ajax($.extend({}, options))
  },
  /**
   * 获取页面所需要的模板
   */
  getTpls (callback) {
    let taskArr = []
    let completeTotal = 0
    let taskTotal = 0
    content = $('body').html()
    let includeArr = content.match(/\{\{include.*?\}\}/g)
    taskTotal = includeArr.length
    function get (includeArr) {
      if (includeArr) {
        includeArr.map(include => {
          let tplName = include.match(/\"(.*)?\"/)[1];
          let id = tplName.match(/(\w|-)+$/)[0]
          // 判断模板是否存在
          if ($('script[type="text/html"][id="' + id + '"]').length) {
            completeTotal++
          } else {
            g.ajax({
              url: './pages/common/' + tplName + '.html',
              type: 'GET',
              success (result) {
                let scriptHtml = `<script type="text/html" id="${tplName}">${result.trim()}</script>`
                $('body').append(scriptHtml)
                let includeCount = scriptHtml.match(/\{\{include.*?\}\}/g) ? scriptHtml.match(/\{\{include.*?\}\}/g).length : 0
                taskTotal += includeCount
                if (includeCount) {
                  get(scriptHtml.match(/\{\{include.*?\}\}/g))
                }
              },
              complete () {
                completeTotal++
              }
            })
          }
        })
      }
    }
    get(includeArr)

    let time = setInterval(() => {
      if (completeTotal == taskTotal) {
        clearInterval(time) 
          callback() 
        
      }
    })
  },
  render (from, to, data, callback) {
    to = ('#' + to).replace('##', '#')
    g.getTpls(() => {
      let html = template(from, data)
      $(to).html(html)
      if (callback) { callback() }
    })
  }
}
